package com.ruoyi.common.utils;

import java.util.Random;

public class RandomUtils {

    private static final String LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String DIGITS = "0123456789";
    private static final String No_Zero_DIGITS = "123456789";

    /**
     * 随机生成数字字符串
     **/
    public static String generateRandomDigits(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(DIGITS.length());
            sb.append(DIGITS.charAt(randomIndex));
        }

        return sb.toString();
    }

    /**
     * 随机生成大小写字母字符串
     **/
    public static String generateRandomLetters(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(LETTERS.length());
            sb.append(LETTERS.charAt(randomIndex));
        }

        return sb.toString();
    }

    /**
     * 随机生成大小写字母+数字的字符串
     **/
    public static String generateRandomMixedString(int length) {
        String validChars = LETTERS + DIGITS;

        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(validChars.length());
            sb.append(validChars.charAt(randomIndex));
        }

        return sb.toString();
    }

    /**
     * 随机生成大小写字母+数字的字符串（数字不包含0）
     **/
    public static String generateRandomNoZeroString(int length) {
        String validChars = LETTERS + No_Zero_DIGITS;

        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(validChars.length());
            sb.append(validChars.charAt(randomIndex));
        }

        return sb.toString();
    }

}
